(function () {
    /*
     * 检查是否有重复
     */
    var is_change = true;
    $("#card").on("change", function () {
        is_change = true;
    });
    $("#card").on("blur", function () {
        if(is_change){
            is_change = false;
            var now_card = $("#card").val();
            var data = {
                "now_card": now_card
            };
            var request_url = package_url("public_module/index/check_info");
            crm_ajax(data, request_url, function (res) {
                if (res) {
                    crm.alert("职员已存在,名字是:" + res, "error", function () {
                        $("#card").focus();
                    })
                }
            });
        }
    })

    /*
     *展示职员详细信息
     */
    $(".staff_show").on("click", function () {
        var now_staff = $(this).parent().parent().attr("data-id");
        var request_url = package_url("public_module/index/get_staff_info");
        var data = {
            "now_staff": now_staff
        };
        crm_ajax(data, request_url, function (res) {
            var popup_html = "<div class='row wrapper info_content'><div class='col-sm-3 info_left'>姓名</div><div class='col-sm-9 info_right'>" + res[0].name + "</div><div class='col-sm-3 info_left'>私人电话号码</div><div class='col-sm-9 info_right'>" + (res[0].private_tel ? res[0].private_tel : 'NULL') + "</div><div class='col-sm-3 info_left'>英文姓名</div><div class='col-sm-9 info_right'>" + (res[0].name_en ? res[0].name_en : 'NULL') + "</div><div class='col-sm-3 info_left'>身份证号码</div><div class='col-sm-9 info_right'>" + (res[0].card ? res[0].card : 'NULL') + "</div><div class='col-sm-3 info_left'>性别</div><div class='col-sm-9 info_right'>" + (res[0].sex == 1 ? '男士' : '女士') + "</div><div class='col-sm-3 info_left'>出生日期</div><div class='col-sm-9 info_right'>" + (res[0].birthday ? res[0].birthday : 'NULL') + "</div><div class='col-sm-3 info_left'>婚姻状况</div><div class='col-sm-9 info_right'>" + (res[0].is_marry == 1 ? '已婚' : '未婚') + "</div><div class='col-sm-3 info_left'>员工籍贯</div><div class='col-sm-9 info_right'>" + (res[0].native_place ? res[0].native_place : 'NULL') + "</div><div class='col-sm-3 info_left'>现居住地址</div><div class='col-sm-9 info_right'>" + (res[0].residence ? res[0].residence : 'NULL') + "</div><div class='col-sm-3 info_left'>学历</div><div class='col-sm-9 info_right'>" + (res[0].grade ? res[0].grade : 'NULL') + "</div><div class='col-sm-3 info_left'>学校</div><div class='col-sm-9 info_right'>" + (res[0].school ? res[0].school : 'NULL') + "</div><div class='col-sm-3 info_left'>专业</div><div class='col-sm-9 info_right'>" + (res[0].major ? res[0].major : 'NULL') + "</div><div class='col-sm-3 info_left'>紧急联系人</div><div class='col-sm-9 info_right'>" + (res[0].emergency_name ? res[0].emergency_name : 'NULL') + "</div><div class='col-sm-3 info_left'>紧急联系人电话</div><div class='col-sm-9 info_right'>" + (res[0].emergency_phone ? res[0].emergency_phone : 'NULL') + "</div><div class='col-sm-3 info_left'>账号</div><div class='col-sm-9 info_right'>" + (res[0].account ? res[0].account : 'NULL') + "</div><div class='col-sm-3 info_left'>密码</div><div class='col-sm-9 info_right'>" + (res[0].password ? res[0].password : 'NULL') + "</div><div class='col-sm-3 info_left'>工作电话号码</div><div class='col-sm-9 info_right'>" + (res[0].work_tel ? res[0].work_tel : 'NULL') + "</div><div class='col-sm-3 info_left'>部门</div><div class='col-sm-9 info_right'>" + (res[0].department_id ? res[0].department_id : 'NULL') + "</div><div class='col-sm-3 info_left'>职位</div><div class='col-sm-9 info_right'>" + (res[0].position_name ? res[0].position_name : 'NULL') + "</div><div class='col-sm-3 info_left'>所属权限组</div><div class='col-sm-9 info_right'>" + (res[0].description ? res[0].description : 'NULL') + "</div><div class='col-sm-3 info_left'>工资卡号</div><div class='col-sm-9 info_right'>" + (res[0].salary_card ? res[0].salary_card : 'NULL') + "</div><div class='col-sm-3 info_left'>工资卡卡户银行</div><div class='col-sm-9 info_right'>" + (res[0].bank_card_account ? res[0].bank_card_account : 'NULL') + "</div><div class='col-sm-3 info_left'>员工工资</div><div class='col-sm-9 info_right'>" + (res[0].money ? res[0].money : 'NULL') + "</div><div class='col-sm-3 info_left'>合同情况</div><div class='col-sm-9 info_right'>" + (res[0].contract_content ? res[0].contract_content : 'NULL') + "</div><div class='col-sm-3 info_left'>社保情况</div><div class='col-sm-9 info_right'>" + (res[0].security_content ? res[0].security_content : 'NULL') + "</div><div class='col-sm-3 info_left'>入职时间</div><div class='col-sm-9 info_right'>" + (res[0].entry_time ? res[0].entry_time : 'NULL') + "</div><div class='col-sm-3 info_left'>私人微信</div><div class='col-sm-9 info_right'>" + (res[0].private_wechat ? res[0].private_wechat : 'NULL') + "</div><div class='col-sm-3 info_left'>工作微信</div><div class='col-sm-9 info_right'>" + (res[0].work_wechat ? res[0].work_wechat : 'NULL') + "</div><div class='col-sm-3 info_left'>私人QQ</div><div class='col-sm-9 info_right'>" + (res[0].private_qq ? res[0].private_qq : 'NULL') + "</div><div class='col-sm-3 info_left'>工作QQ</div><div class='col-sm-9 info_right'>" + (res[0].work_qq ? res[0].work_qq : 'NULL') + "</div><div class='col-sm-3 info_left'>家庭情况</div><div class='col-sm-9 info_right'>" + (res[0].family_situation ? res[0].family_situation : 'NULL') + "</div></div><div class='popup_bottom'><a href='/public_module/index/show_add_edit/now_staff/" + now_staff + "' class='btn btn-s-md btn-default'>修 改</a></div>";
            popup_window(res[0].name + "的个人信息", popup_html);
        })
    })

    /*
     * 所属部门改变的时候
     */
    $('select[name="department_id"]').on('change', function () {
        var department_position = $(this).val();
        var data = {
                "id": department_position,
                "act": "get_department_position"//部门表里面所属部门的id
            },
            request_url = package_url("public_module/index/show_add_edit"),
            callback = function (res) {
                //清空所属职位
                $('select[name="department_position_id"]').children().remove();
                for (var i = 0; i < res.length; i++) {
                    var options = '<option value="' + res[i].id + '">' + res[i].position_name + '</option>';
                    $('select[name="department_position_id"]').append(options);
                }
            };
        crm_ajax(data, request_url, callback);
    });

    /*
     * 删除职员
     */
    $(".staff_delete").on("click", function () {
        var _s = $(this);
        crm.alert("是否要删除?", "warning", function () {
            var now_staff = _s.parent().parent().attr("data-id");
            var data = {
                "id": now_staff
            };
            var request_url = package_url("public_module/index/delete");
            var order_url = package_url("public_module/index/show_list");
            var callback = function (res) {
                if (res.status == 1) {
                    crm.alert(res.msg, "success", function () {
                        location.href = order_url;
                    });
                } else {
                    crm.alert(res.msg, "error", function () {
                        location.href = order_url;
                    });
                }
            };
            crm_ajax(data, request_url, callback);
        }, true);
    })

    /*
     * 恢复咨询者
     */
    $(".restore").on("click", function () {
        var _s = $(this);
        crm.alert("是否要恢复?", "warning", function () {
            var now_staff = _s.parent().parent().attr("data-id");
            var data = {
                "id": now_staff
            };
            var request_url = package_url("public_module/index/confirm_restore");
            var order_url = package_url("public_module/index/show_restore");
            var callback = function (res) {
                if (res.status == 1) {
                    crm.alert(res.msg, "success", function () {
                        location.href = order_url;
                    });
                } else {
                    crm.alert(res.msg, "error", function () {
                        location.href = order_url;
                    });
                }
            };
            crm_ajax(data, request_url, callback);
        }, true);
    })

    /*
     * 添加部门
     */
    $(".department_add").on("click", function() {
        var new_dp_name = $(".new_department").val();
        if(new_dp_name != "") {
            var request_url = package_url("public_module/index/add_department");
            var data = {
                "now_department_name": new_dp_name
            };
            crm_ajax(data, request_url, function(res) {
                if(res.status == 1) {
                    var order_url = package_url("public_module/index/department_show_list");
                    crm.alert(res.msg, "success", function() {
                        location.href = order_url;
                    });
                } else {
                    crm.alert(res.msg, "error");
                }
            })
        } else {
            crm.alert("新部门名不能为空", "error");
        }
    })

    /*
     * 点击修改弹出弹窗
     */

    // crm.dialog("bluejmask", "w5h6", "部门列表");
    // $(".staff_change").each(function () {
    //     $(this).on("click", function () {
    //         $(".mask").css("display", "block");
    //         var department_name = $(this).parent().parent().data("id");
    //         var data = {
    //             "department_name": department_name
    //         };
    //         var request_url = package_url("public_module/index/department_edit");
    //         var order_url = package_url("public_module/index/department_show_list");
    //         var callback = function (res) {
    //             $('input[name="department_name"]').val(res.name);
    //             $('input[name="department_edit_id"]').val(res.id);
    //         };
    //         crm_ajax(data, request_url, callback);
    //     })
    // })

})();




